package dk.zpon.andnoise;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PlaylistView extends View {
	
	String mArtist;
	Paint mArtistPaint;
	String mAlbum;
	Paint mAlbumPaint;
	String mTitle;
	Paint mTitlePaint;
	Paint mTextPaint;
	
	private String mPreArtist;
	Paint mPreArtistPaint;
	private String mPreAlbum;
	Paint mPreAlbumPaint;
	int mLinePadding;

	public PlaylistView(Context context) {
		super(context);
		initiate();
	}
	
	public PlaylistView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initiate();
	}
	
	private void initiate()
	{
		//int lightgray = Color.rgb(189, 189, 189);
		//Color.rgb(120, 120, 120)
		mTitlePaint = new Paint();
		mTitlePaint.setTextSize(16);
		mTitlePaint.setAntiAlias(true);
		mTitlePaint.setColor(Color.WHITE);
		mTitlePaint.setFakeBoldText(true);
		mPreArtistPaint = new Paint();
		mPreArtistPaint.setTextSize(14);
		mPreArtistPaint.setAntiAlias(true);
		mPreArtistPaint.setColor(Color.GRAY);
		mArtistPaint = new Paint();
		mArtistPaint.setTextSize(14);
		mArtistPaint.setAntiAlias(true);
		mArtistPaint.setColor(Color.LTGRAY);
		mPreAlbumPaint = new Paint();
		mPreAlbumPaint.setTextSize(14);
		mPreAlbumPaint.setAntiAlias(true);
		mPreAlbumPaint.setColor(Color.GRAY);
		mAlbumPaint = new Paint();
		mAlbumPaint.setTextSize(14);
		mAlbumPaint.setAntiAlias(true);
		mAlbumPaint.setColor(Color.LTGRAY);
		
		/*
		 * Order:
		 * 	mTitle
		 * 	mPreArtist + mArtist + mPreAlbum + mAlbum
		 */
		mTitle = "Title of Trackgjq";
		mArtist = "Artist";
		mAlbum = "Album";
		mPreArtist = "by ";
		mPreAlbum = " on ";
		mLinePadding = 0;
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		int width = this.getPaddingLeft();
		canvas.drawText(mTitle, width, (int)(-mTitlePaint.ascent())+this.getPaddingTop(), mTitlePaint);
		int height = (int)(-mTitlePaint.ascent() + mTitlePaint.descent() + -mArtistPaint.ascent() + mLinePadding)+this.getPaddingTop();
		canvas.drawText(mPreArtist, width, height, mPreArtistPaint);
		width += (int)mPreArtistPaint.measureText(mPreArtist);
		canvas.drawText(mArtist, width, height, mArtistPaint);
		width += (int)mArtistPaint.measureText(mArtist);
		canvas.drawText(mPreAlbum, width, height, mPreAlbumPaint);
		width += (int)mAlbumPaint.measureText(mPreAlbum);
		canvas.drawText(mAlbum, width, height, mAlbumPaint);
	}
	
	@Override
	protected void onMeasure(int width, int height)
	{		
		this.setMeasuredDimension(measureWidth(width), measureHeight(height));
	}
	
	/**
	 * Modified version of LabelView.java found in ApiDemos of android sdk 
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
        	int line1 = (int) mTitlePaint.measureText(mTitle) + getPaddingLeft() + getPaddingRight();
        	int line2 = (int) mPreArtistPaint.measureText(mPreArtist) + (int) mArtistPaint.measureText(mArtist) +
        				(int) mPreAlbumPaint.measureText(mPreAlbum) + (int) mAlbumPaint.measureText(mAlbum);
            result = (line1 < line2 ? line2 : line1);
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Modified version of LabelView.java found in ApiDemos of android sdk
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = 	(int) (Math.ceil(-mTitlePaint.ascent() + mTitlePaint.descent())) + mLinePadding +
            			(int) (Math.ceil(-mArtistPaint.ascent() + mArtistPaint.descent())) +
            			getPaddingTop() +
            			getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
    public void setData(String title, String artist, String album)
    {
    	this.mTitle = title;
    	this.mArtist = artist;
    	this.mAlbum = album;
    	
    	requestLayout();
        invalidate();
    }

}
